package action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.springframework.context.support.ResourceBundleMessageSource;
import po.User;
import service.NewsService;
import service.UserService;

/**
 *
 * @author mg
 */
public class LoginAction extends ActionSupport implements ServletRequestAware {

    private UserService userService;
    private NewsService newsService;
    private String message;
    private String sideBar;
    private int userId;
    private String password;
    private ResourceBundleMessageSource messageSource;
    private HttpServletRequest request;

    public LoginAction() {
    }


    @Override
    public String execute() throws Exception {
        User usr = userService.getUser(userId);
        if (usr != null) {
            if (usr.getPassword().equals(password)) {
                ActionContext.getContext().getSession().put("user", usr);
                ActionContext.getContext().getSession().put("right", usr.getType()+"");
                sideBar = userService.getSideBar(userId);
               ActionContext.getContext().getSession().put("sideBar", sideBar);

                return usr.getType()==1?"manager":"user";

            } else {
                message = messageSource.getMessage("pswError", null, Locale.CHINA);
                return ERROR;
            }
        } else {
            message = messageSource.getMessage("usrError", null, Locale.CHINA);
            return ERROR;

        }

    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setMessageSource(ResourceBundleMessageSource messageSource) {
        this.messageSource = messageSource;
    }

    public void setNewsService(NewsService newsService) {
        this.newsService = newsService;
    }

    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
    }


    public String getSideBar() {
        return sideBar;
    }

    public void setSideBar(String sideBar) {
        this.sideBar = sideBar;
    }
}

